package com.scs.application.modules.order.controller;/**
 * @Description:
 */

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.mvc.controller.BaseController;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.order.consts.DictConsts;
import com.scs.application.modules.order.entity.Delivery;
import com.scs.application.modules.order.entity.PurchaseOrder;
import com.scs.application.modules.order.entity.PurchaseProcess;
import com.scs.application.modules.order.request.PurchaseOrderQueryRequest;
import com.scs.application.modules.order.service.DeliveryService;
import com.scs.application.modules.order.service.PurchaseOrderService;
import com.scs.application.modules.order.service.PurchaseProcessService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName WorkbenchController
 * @Description TODO
 */
@AllArgsConstructor
@RestController
@RequestMapping("/order/workbench")
public class OrderWorkbenchController extends BaseController {
    private PurchaseOrderService purchaseOrderService;

    private DeliveryService deliveryService;

    private PurchaseProcessService purchaseProcessService;

    /**
     * 供应商工作台 订单相关内容 待确认/待发货/已发货/已送达订单
     *
     * @return
     */
    @GetMapping("/count_for_supplier")
    public CommonResult count() {
        HashMap<String, Integer> map = Maps.newHashMapWithExpectedSize(4);

        // 待确认订单数
        int unconfirmCnt = purchaseOrderService.count(Wrappers.<PurchaseOrder>query()
                .eq("supplier_id", UserUtils.getSupplierId())
                .eq("status", DictConsts.STATUS_SUPPLIER_UNCONFIRM));
        map.put("unconfirmCnt", unconfirmCnt);

        // 待发货订单数
        int undeliveryCnt = deliveryService.count(Wrappers.<Delivery>query()
                .eq("supplier_id", UserUtils.getSupplierId())
                .eq("status", DictConsts.STATUS_DELIVERY_UNFILLED));
        map.put("undeliveryCnt", undeliveryCnt);

       /* // 已发货订单数
        int deliveryedCnt = purchaseOrderService.count(Wrappers.<PurchaseOrder>query()
                .eq("supplier_id", UserUtils.getSupplierId())
                .eq("status", DictConsts.STATUS_SUPPLIER_DELIVERYED));
        map.put("deliveryedCnt", deliveryedCnt);

        // 已送达订单数
        int completeCnt = purchaseOrderService.count(Wrappers.<PurchaseOrder>query()
                .eq("supplier_id", UserUtils.getSupplierId())
                .eq("status", DictConsts.STATUS_COMPLETE));
        map.put("completeCnt", completeCnt);*/
        return CommonResult.ok("查询成功", map);
    }

    /**
     * 医院工作台 订单相关内容 待确认/待审核
     *
     * @return
     */
    @GetMapping("/count_for_hospital")
    public CommonResult countForHospital() {
        HashMap<String, Integer> map = Maps.newHashMapWithExpectedSize(2);

        // 待提交订单数
        int unconfirmCnt = purchaseOrderService.count(Wrappers.<PurchaseOrder>query()
                .eq("hospital_id", UserUtils.getHospitalId())
                .eq("status", DictConsts.STATUS_UNSUBMIT)
                .eq("is_closed", DictConsts.CLOSED_NO)
        );
        map.put("unconfirmCnt", unconfirmCnt);

        // 待审核订单数
        int unpassCnt = purchaseOrderService.count(Wrappers.<PurchaseOrder>query()
                .eq("supplier_id", UserUtils.getHospitalId())
                .eq("status", DictConsts.STATUS_UNPASS)
                .eq("is_closed", DictConsts.CLOSED_NO)
        );
        map.put("unpassCnt", unpassCnt);

        return CommonResult.ok("查询成功", map);
    }

    @GetMapping("/count_order_for_hospital")
    public CommonResult countOrderForHospital(@Param("request") PurchaseOrderQueryRequest request) {
        HashMap<String, String> map = Maps.newHashMapWithExpectedSize(6);

        //已发货订单
        int orderSend = purchaseOrderService.orderSendCount(request);
        map.put("orderSend", String.valueOf(orderSend));

        //今日已发货金额
        PurchaseOrder purchaseOrder = purchaseOrderService.selectDeliveryCount(request);
        if (purchaseOrder == null) {
            map.put("deliveryAmount", "0");
        } else {
            map.put("deliveryAmount", String.valueOf(purchaseOrder.getDeliveryAmount()));
        }

        //今日可供货订单
        int availableOrders = purchaseOrderService.availableOrdersCount(request);
        map.put("availableOrders", String.valueOf(availableOrders));

        //今日已发货订单
        int delivery = purchaseOrderService.deliveryCount(request);
        map.put("delivery", String.valueOf(delivery));

        //今日已完成订单
        int finished = purchaseOrderService.finishedCount(request);
        map.put("finished", String.valueOf(finished));

        //已完成订单金额
        PurchaseOrder p = purchaseOrderService.finishedAmountCount(request);
        map.put("finishedAmount", String.valueOf(p == null ? "0" : p.getDeliveryAmount()));
        return CommonResult.ok("查询成功", map);
    }

    @GetMapping("/purchase_process")
    public CommonResult purchaseOrderForHospital() {
        List<PurchaseProcess> list = Lists.newArrayList();
        if (UserUtils.isSupplier()) {
            list = purchaseProcessService.list(Wrappers.<PurchaseProcess>query()
                    .eq("supplier_id", UserUtils.getSupplierId())
                    .orderByDesc("gmt_create").last("limit 100"));
        } else if (UserUtils.isHospital()) {
            list = purchaseProcessService.list(Wrappers.<PurchaseProcess>query()
                    .eq("hospital_id", UserUtils.getHospitalId())
                    .orderByDesc("gmt_create").last("limit 100"));
        }
        // List<PurchaseOrder> purchaseOrders = purchaseOrderService.orderSendStatus();
       /* List<PurchaseProcess> list = purchaseProcessService.list(Wrappers.<PurchaseProcess>query()
                .eq("hospital_id",UserUtils.getHospitalId())
                .orderByDesc("gmt_create").last("limit 100"));*/
        return CommonResult.ok("查询成功", list);
    }

    @GetMapping("/count_order_for_supplier")
    public CommonResult countOrderForSupplier(@Param("request") PurchaseOrderQueryRequest request) {
        HashMap<String, String> map = Maps.newHashMapWithExpectedSize(6);
        //已接收订单
       /* int orderSend = purchaseOrderService.orderSendCountSupplier(request);
        map.put("orderSend", String.valueOf(orderSend));
        //待发货金额
        PurchaseOrder purchaseOrder = purchaseOrderService.selectDeliveryCountSupplier(request);
        if (purchaseOrder == null) {
            map.put("deliveryAmountDay", "0");
        } else {
            map.put("deliveryAmountDay", String.valueOf(purchaseOrder.getDelivaryAmount()));
        }*/
        //今日可供货订单
        int availableOrders = purchaseOrderService.availableOrdersCountSupplier(request);
        map.put("availableOrders", String.valueOf(availableOrders));
        //今日已发货订单

        int delivery = purchaseOrderService.deliveryCountSupplier(request);
        map.put("delivery", String.valueOf(delivery));

        //今日已完成订单
        int finished = purchaseOrderService.finishedCountSupplier(request);
        map.put("finished", String.valueOf(finished));

        PurchaseOrder p = purchaseOrderService.finishedAmountCountSupplier(request);
        map.put("finishedAmount", String.valueOf(p == null ? "0" : p.getDeliveryAmount()));
        return CommonResult.ok("查询成功", map);
    }

}
